package com.itheima.controller.backend.reservation;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.constant.RedisConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.AliOssUtil;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetMealService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
 * @className SetMealController
 */
@RestController
@RequestMapping("/setmeal")
@RequiredArgsConstructor
public class SetMealController {

    private final SetMealService setMealService;
    private final AliOssUtil aliOssUtil;
    private final RedisTemplate<String,String> redisTemplate;

    /**
     * 添加套餐
     * @param checkGroupIds 检查组id集合
     * @param setMeal 套餐
     * @return Result
     **/
    @PostMapping("/add")
    public Result addSetMeal(@RequestParam("checkgroupIds") List<Integer> checkGroupIds,
                             @RequestBody Setmeal setMeal){
        setMealService.addSetMeal(checkGroupIds,setMeal);
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    /**
     * 删除套餐
     * @param setMealId 套餐id
     * @return Result
     **/
    @GetMapping("/deleteById")
    public Result deleteSetMealById(@RequestParam("id") Integer setMealId){
        setMealService.deleteSetMealById(setMealId);
        return new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
    }

    /**
     * 编辑套餐
     * @param checkGroupIds 检查组id集合
     * @param setMeal 套餐
     * @return Result
     **/
    @PostMapping("/edit")
    public Result editSetMeal(@RequestParam("checkGroupIds") List<Integer> checkGroupIds,
                              @RequestBody Setmeal setMeal){
        setMealService.editSetMeal(checkGroupIds,setMeal);
        return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
    }

    /**
     * 根据套餐id查询套餐
     * @param setMealId 套餐id
     * @return Result
     **/
    @GetMapping("/findById")
    public Result getSetMealById(@RequestParam("id") Integer setMealId){
        Setmeal setMeal = setMealService.getSetMealById(setMealId);
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,setMeal);
    }

    /**
     * 分页查询套餐
     * @param queryPageBean 分页条件
     * @return Result
     **/
    @PostMapping("/findPage")
    public PageResult getSetMealByPage(@RequestBody QueryPageBean queryPageBean){
        return setMealService.getSetMealByPage(queryPageBean);
    }

    /**
     * 上传图片
     * @param imgFile 文件
     * @return Result
     **/
    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile) throws IOException {
        //原始名
        String originalFilename = imgFile.getOriginalFilename();
        //后缀
        String suffix = Objects.requireNonNull(originalFilename).substring(originalFilename.lastIndexOf("."));
        //新文件名
        String newFileName = UUID.randomUUID() + suffix;
        //文件上传
        String filePath = aliOssUtil.upload(imgFile.getBytes(), newFileName);
        //将所有上传的图片名（包含已入库和未入库）保存到redis
        redisTemplate.opsForSet().add(RedisConstant.UPLOADED_IMAGES_KEY,newFileName);
        return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, filePath);
    }
}
